﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CloundUploader.Framework.Log;
using CloundUploader.Framework.UI;
using CloundUploader.Framework.User;
using MyUploader.UI;

namespace CloundUploader.Framework
{
   public class UploaderContext
   {
       public const string ClientName = "CloundUploader";

       public static readonly UploaderContext Instance;

       public ILog Log { get;private set; }
      
       static UploaderContext()
       {
           Instance = new UploaderContext();
           
       }

       public User.User User { get; set; }
       public MainForm MainForm { get; set; }
       private UploaderContext()
       {
           Log = new Log.Log();
           if (User == null)
           {
               User = new User.User();
               User.UploadSetting = new UserUploadSetting();
           }
       }

       public void Start()
       {
           UserCheck userCheck = new UserCheck();
           userCheck.UserCheckSucessEvent += (o) =>
                                                 {
               var user = o as UserServices.UserInfo;
               UploaderContext.Instance.User = new User.User()
               {
                   Identity = user.Identity,
                   Name = user.Name,
                   Pass = user.Password,
                   IsLogin = true,
                   UploadSetting = new UserUploadSetting()
                   {
                       AutoStart = user.UploadSetting.AutoStart,
                       AutoShutUp = user.UploadSetting.AutoShutUp,
                       FileMax = user.UploadSetting.FileMax,
                       FileMin = user.UploadSetting.FileMin,
                       MaxJob = user.UploadSetting.MaxJob,
                       MaxRetries = user.UploadSetting.MaxRetries,
                       RetryDelay = user.UploadSetting.RetryDelay,
                       Web = user.UploadSetting.Web.ToList()

                   }
               };
               MainForm = new MainForm();
           };
           userCheck.ShowDialog();

           if (User == null || User.Identity==null)
           {
               User = new User.User();
               User.UploadSetting = new UserUploadSetting();
           }
           else
           {
               //MainForm.ShowDialog();
               Application.Run(MainForm);
           }
       }

      
   }
}
